home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / browser.jar / content / browser / preferences / permissions.js < prev    next >
Encoding:
Text File  |  2009-06-24  |  13.1 KB  |  382 lines

  1. //@line 39 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\permissions.js"
  2.  
  3. const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
  4. const nsICookiePermission = Components.interfaces.nsICookiePermission;
  5.  
  6. function Permission(host, rawHost, type, capability, perm) 
  7. {
  8.   this.host = host;
  9.   this.rawHost = rawHost;
  10.   this.type = type;
  11.   this.capability = capability;
  12.   this.perm = perm;
  13. }
  14.  
  15. var gPermissionManager = {
  16.   _type         : "",
  17.   _permissions  : [],
  18.   _pm           : Components.classes["@mozilla.org/permissionmanager;1"]
  19.                             .getService(Components.interfaces.nsIPermissionManager),
  20.   _bundle       : null,
  21.   _tree         : null,
  22.   
  23.   _view: {
  24.     _rowCount: 0,
  25.     get rowCount() 
  26.     { 
  27.       return this._rowCount; 
  28.     },
  29.     getCellText: function (aRow, aColumn)
  30.     {
  31.       if (aColumn.id == "siteCol")
  32.         return gPermissionManager._permissions[aRow].rawHost;
  33.       else if (aColumn.id == "statusCol")
  34.         return gPermissionManager._permissions[aRow].capability;
  35.       return "";
  36.     },
  37.  
  38.     isSeparator: function(aIndex) { return false; },
  39.     isSorted: function() { return false; },
  40.     isContainer: function(aIndex) { return false; },
  41.     setTree: function(aTree){},
  42.     getImageSrc: function(aRow, aColumn) {},
  43.     getProgressMode: function(aRow, aColumn) {},
  44.     getCellValue: function(aRow, aColumn) {},
  45.     cycleHeader: function(column) {},
  46.     getRowProperties: function(row,prop){},
  47.     getColumnProperties: function(column,prop){},
  48.     getCellProperties: function(row,column,prop){
  49.       if (column.element.getAttribute("id") == "siteCol")
  50.         prop.AppendElement(this._ltrAtom);
  51.     }
  52.   },
  53.   
  54.   _getCapabilityString: function (aCapability)
  55.   {
  56.     var stringKey = null;
  57.     switch (aCapability) {
  58.     case nsIPermissionManager.ALLOW_ACTION:
  59.       stringKey = "can";
  60.       break;
  61.     case nsIPermissionManager.DENY_ACTION:
  62.       stringKey = "cannot";
  63.       break;
  64.     case nsICookiePermission.ACCESS_SESSION:
  65.       stringKey = "canSession";
  66.       break;
  67.     }
  68.     return this._bundle.getString(stringKey);
  69.   },
  70.   
  71.   addPermission: function (aCapability)
  72.   {
  73.     var textbox = document.getElementById("url");
  74.     var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
  75.     try {
  76.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  77.                                 .getService(Components.interfaces.nsIIOService);
  78.       var uri = ioService.newURI("http://"+host, null, null);
  79.       host = uri.host;
  80.     } catch(ex) {
  81.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  82.                                     .getService(Components.interfaces.nsIPromptService);
  83.       var message = this._bundle.getString("invalidURI");
  84.       var title = this._bundle.getString("invalidURITitle");
  85.       promptService.alert(window, title, message);
  86.       return;
  87.     }
  88.  
  89.     var capabilityString = this._getCapabilityString(aCapability);
  90.  
  91.     // check whether the permission already exists, if not, add it
  92.     var exists = false;
  93.     for (var i = 0; i < this._permissions.length; ++i) {
  94.       if (this._permissions[i].rawHost == host) {
  95.         // Avoid calling the permission manager if the capability settings are
  96.         // the same. Otherwise allow the call to the permissions manager to
  97.         // update the listbox for us.
  98.         exists = this._permissions[i].perm == aCapability;
  99.         break;
  100.       }
  101.     }
  102.     
  103.     if (!exists) {
  104.       host = (host.charAt(0) == ".") ? host.substring(1,host.length) : host;
  105.       var uri = ioService.newURI("http://" + host, null, null);
  106.       this._pm.add(uri, this._type, aCapability);
  107.     }
  108.     textbox.value = "";
  109.     textbox.focus();
  110.  
  111.     // covers a case where the site exists already, so the buttons don't disable
  112.     this.onHostInput(textbox);
  113.  
  114.     // enable "remove all" button as needed
  115.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  116.   },
  117.   
  118.   onHostInput: function (aSiteField)
  119.   {
  120.     document.getElementById("btnSession").disabled = !aSiteField.value;
  121.     document.getElementById("btnBlock").disabled = !aSiteField.value;
  122.     document.getElementById("btnAllow").disabled = !aSiteField.value;
  123.   },
  124.   
  125.   onHostKeyPress: function (aEvent)
  126.   {
  127.     if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
  128.       document.getElementById("btnAllow").click();
  129.   },
  130.   
  131.   onLoad: function ()
  132.   {
  133.     this._bundle = document.getElementById("bundlePreferences");
  134.     var params = window.arguments[0];
  135.     this.init(params);
  136.   },
  137.   
  138.   init: function (aParams)
  139.   {
  140.     if (this._type) {
  141.       // reusing an open dialog, clear the old observer
  142.       this.uninit();
  143.     }
  144.  
  145.     this._type = aParams.permissionType;
  146.     this._manageCapability = aParams.manageCapability;
  147.     
  148.     var permissionsText = document.getElementById("permissionsText");
  149.     while (permissionsText.hasChildNodes())
  150.       permissionsText.removeChild(permissionsText.firstChild);
  151.     permissionsText.appendChild(document.createTextNode(aParams.introText));
  152.  
  153.     document.title = aParams.windowTitle;
  154.     
  155.     document.getElementById("btnBlock").hidden    = !aParams.blockVisible;
  156.     document.getElementById("btnSession").hidden  = !aParams.sessionVisible;
  157.     document.getElementById("btnAllow").hidden    = !aParams.allowVisible;
  158.  
  159.     var urlFieldVisible = (aParams.blockVisible || aParams.sessionVisible || aParams.allowVisible);
  160.  
  161.     var urlField = document.getElementById("url");
  162.     urlField.value = aParams.prefilledHost;
  163.     urlField.hidden = !urlFieldVisible;
  164.  
  165.     this.onHostInput(urlField);
  166.  
  167.     var urlLabel = document.getElementById("urlLabel");
  168.     urlLabel.hidden = !urlFieldVisible;
  169.  
  170.     var os = Components.classes["@mozilla.org/observer-service;1"]
  171.                        .getService(Components.interfaces.nsIObserverService);
  172.     os.addObserver(this, "perm-changed", false);
  173.  
  174.     if (this._type == "install") {
  175.       var enumerator = this._pm.enumerator;
  176.       if (!enumerator.hasMoreElements())
  177.         this._updatePermissions();
  178.     }
  179.  
  180.     this._loadPermissions();
  181.     
  182.     urlField.focus();
  183.  
  184.     this._ltrAtom = Components.classes["@mozilla.org/atom-service;1"]
  185.                               .getService(Components.interfaces.nsIAtomService)
  186.                               .getAtom("ltr");
  187.   },
  188.   
  189.   uninit: function ()
  190.   {
  191.     var os = Components.classes["@mozilla.org/observer-service;1"]
  192.                        .getService(Components.interfaces.nsIObserverService);
  193.     os.removeObserver(this, "perm-changed");
  194.   },
  195.   
  196.   observe: function (aSubject, aTopic, aData)
  197.   {
  198.     if (aTopic == "perm-changed") {
  199.       var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
  200.       if (aData == "added") {
  201.         this._addPermissionToList(permission);
  202.         ++this._view._rowCount;
  203.         this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);        
  204.         // Re-do the sort, since we inserted this new item at the end. 
  205.         gTreeUtils.sort(this._tree, this._view, this._permissions, 
  206.                         this._lastPermissionSortColumn, 
  207.                         this._lastPermissionSortAscending);        
  208.       }
  209.       else if (aData == "changed") {
  210.         for (var i = 0; i < this._permissions.length; ++i) {
  211.           if (this._permissions[i].host == permission.host) {
  212.             this._permissions[i].capability = this._getCapabilityString(permission.capability);
  213.             break;
  214.           }
  215.         }
  216.         // Re-do the sort, if the status changed from Block to Allow
  217.         // or vice versa, since if we're sorted on status, we may no
  218.         // longer be in order. 
  219.         if (this._lastPermissionSortColumn.id == "statusCol") {
  220.           gTreeUtils.sort(this._tree, this._view, this._permissions, 
  221.                           this._lastPermissionSortColumn, 
  222.                           this._lastPermissionSortAscending);
  223.         }
  224.         this._tree.treeBoxObject.invalidate();
  225.       }
  226.       // No UI other than this window causes this method to be sent a "deleted"
  227.       // notification, so we don't need to implement it since Delete is handled
  228.       // directly by the Permission Removal handlers. If that ever changes, those
  229.       // implementations will have to move into here. 
  230.     }
  231.   },
  232.   
  233.   onPermissionSelected: function ()
  234.   {
  235.     var hasSelection = this._tree.view.selection.count > 0;
  236.     var hasRows = this._tree.view.rowCount > 0;
  237.     document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
  238.     document.getElementById("removeAllPermissions").disabled = !hasRows;
  239.   },
  240.   
  241.   onPermissionDeleted: function ()
  242.   {
  243.     if (!this._view.rowCount)
  244.       return;
  245.     var removedPermissions = [];
  246.     gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
  247.     for (var i = 0; i < removedPermissions.length; ++i) {
  248.       var p = removedPermissions[i];
  249.       this._pm.remove(p.host, p.type);
  250.     }    
  251.     document.getElementById("removePermission").disabled = !this._permissions.length;
  252.     document.getElementById("removeAllPermissions").disabled = !this._permissions.length;
  253.   },
  254.   
  255.   onAllPermissionsDeleted: function ()
  256.   {
  257.     if (!this._view.rowCount)
  258.       return;
  259.     var removedPermissions = [];
  260.     gTreeUtils.deleteAll(this._tree, this._view, this._permissions, removedPermissions);
  261.     for (var i = 0; i < removedPermissions.length; ++i) {
  262.       var p = removedPermissions[i];
  263.       this._pm.remove(p.host, p.type);
  264.     }    
  265.     document.getElementById("removePermission").disabled = true;
  266.     document.getElementById("removeAllPermissions").disabled = true;
  267.   },
  268.   
  269.   onPermissionKeyPress: function (aEvent)
  270.   {
  271.     if (aEvent.keyCode == 46)
  272.       this.onPermissionDeleted();
  273.   },
  274.   
  275.   _lastPermissionSortColumn: "",
  276.   _lastPermissionSortAscending: false,
  277.   
  278.   onPermissionSort: function (aColumn)
  279.   {
  280.     this._lastPermissionSortAscending = gTreeUtils.sort(this._tree, 
  281.                                                         this._view, 
  282.                                                         this._permissions,
  283.                                                         aColumn, 
  284.                                                         this._lastPermissionSortColumn, 
  285.                                                         this._lastPermissionSortAscending);
  286.     this._lastPermissionSortColumn = aColumn;
  287.   },
  288.   
  289.   _loadPermissions: function ()
  290.   {
  291.     this._tree = document.getElementById("permissionsTree");
  292.     this._permissions = [];
  293.  
  294.     // load permissions into a table
  295.     var count = 0;
  296.     var enumerator = this._pm.enumerator;
  297.     while (enumerator.hasMoreElements()) {
  298.       var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
  299.       this._addPermissionToList(nextPermission);
  300.     }
  301.    
  302.     this._view._rowCount = this._permissions.length;
  303.  
  304.     // sort and display the table
  305.     this._tree.treeBoxObject.view = this._view;
  306.     this.onPermissionSort("rawHost", false);
  307.  
  308.     // disable "remove all" button if there are none
  309.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  310.   },
  311.   
  312.   _addPermissionToList: function (aPermission)
  313.   {
  314.     if (aPermission.type == this._type &&
  315.         (!this._manageCapability ||
  316.          (aPermission.capability == this._manageCapability))) {
  317.  
  318.       var host = aPermission.host;
  319.       var capabilityString = this._getCapabilityString(aPermission.capability);
  320.       var p = new Permission(host,
  321.                              (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
  322.                              aPermission.type,
  323.                              capabilityString, 
  324.                              aPermission.capability);
  325.       this._permissions.push(p);
  326.     }  
  327.   },
  328.   
  329.   _updatePermissions: function ()
  330.   {
  331.     try {
  332.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  333.                                 .getService(Components.interfaces.nsIIOService);
  334.       var pbi = Components.classes["@mozilla.org/preferences-service;1"]
  335.                           .getService(Components.interfaces.nsIPrefBranch2);
  336.       var prefList = [["xpinstall.whitelist.add", nsIPermissionManager.ALLOW_ACTION],
  337.                       ["xpinstall.whitelist.add.103", nsIPermissionManager.ALLOW_ACTION],
  338.                       ["xpinstall.blacklist.add", nsIPermissionManager.DENY_ACTION]];
  339.  
  340.       for (var i = 0; i < prefList.length; ++i) {
  341.         try {
  342.           // this pref is a comma-delimited list of hosts
  343.           var hosts = pbi.getCharPref(prefList[i][0]);
  344.         } catch(ex) {
  345.           continue;
  346.         }
  347.  
  348.         if (!hosts)
  349.           continue;
  350.  
  351.         hostList = hosts.split(",");
  352.         var capability = prefList[i][1];
  353.         for (var j = 0; j < hostList.length; ++j) {
  354.           // trim leading and trailing spaces
  355.           var host = hostList[j].replace(/^\s*/,"").replace(/\s*$/,"");
  356.           try {
  357.             var uri = ioService.newURI("http://" + host, null, null);
  358.             this._pm.add(uri, this._type, capability);
  359.           } catch(ex) { }
  360.         }
  361.         pbi.setCharPref(prefList[i][0], "");
  362.       }
  363.     } catch(ex) { }
  364.   },
  365.   
  366.   setHost: function (aHost)
  367.   {
  368.     document.getElementById("url").value = aHost;
  369.   }
  370. };
  371.  
  372. function setHost(aHost)
  373. {
  374.   gPermissionManager.setHost(aHost);
  375. }
  376.  
  377. function initWithParams(aParams)
  378. {
  379.   gPermissionManager.init(aParams);
  380. }
  381.  
  382.